import moment from 'moment';
import { defineConfig } from 'umi';
import defaultSettings from './defaultSettings';
import proxy from './proxy';
import routes from './routes';

const path = require('path');
const formattedDate = moment().format('YYYYMMDDHHmm');
const { REACT_APP_ENV } = process.env;

export default defineConfig({
  plugins: [
    '@umijs/plugins/dist/initial-state',
    '@umijs/plugins/dist/model',
    '@umijs/plugins/dist/antd',
    '@umijs/plugins/dist/locale',
    '@umijs/plugins/dist/request',
    '@umijs/plugins/dist/layout',
  ],
  model: {},
  initialState: {},
  history: {
    type: 'hash', // 为了解决在非根目录下部署问题，采用hash路由：#/xxx的形式
  },
  // base: process.env.NODE_ENV === 'production' ? './' : '/', 这个加了之后，会在路由上多出一个  './' 如：/#/./welcome
  publicPath: process.env.NODE_ENV === 'production' ? './' : '/', // nginx非根目录下部署
  hash: true, // 是否开启 hash 文件后缀
  theme: {
    'primary-color': defaultSettings.primaryColor,
  },
  title: false,
  antd: {},
  request: {},
  fastRefresh: true,
  ignoreMomentLocale: true,
  proxy: proxy[REACT_APP_ENV || 'dev'],
  manifest: {
    basePath: '/',
  },
  mfsu: false,
  alias: {
    '~': path.resolve(__dirname, '../'),
  },
  locale: {
    // default zh-CN
    default: 'zh-CN',
    antd: true,
    // default true, when it is true, will use `navigator.language` overwrite default
    baseNavigator: true,
  },
  layout: {
    locale: true,
    siderWidth: 228,
    ...defaultSettings,
  },
  routes,
  chainWebpack(config) {
    //加日期
    if (process.env.NODE_ENV === 'production') {
      config.output.filename(`[name].[contenthash:8].${formattedDate}.js`);
      config.output.chunkFilename(`[name].[contenthash:8].${formattedDate}.async.js`);
    }

    return config;
  },
  // chainWebpack(config, args) {
  //   // config.externals({
  //   //   'public': /^public\/.*/,
  //   // });
  //
  //   config.module.rule('exclude-library')
  //       .exclude.add(/public/)
  //       .end();
  //   return config;
  // },
});
